//
//  OptionLayer.m
//  Maze1
//
//  Created by student on 11/4/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "OptionLayer.h"
#import "ClientState.h"
#define SAVEBUTTON 0
#define QUITBUTTON 1
#define NOTIFICATION 2
#define SOUNDON 3
#define SOUNDOFF 4
#define OPTIONMENU 5

@implementation OptionLayer
@synthesize bg;
@synthesize soundon;
@synthesize _plusItem;
@synthesize _plusItem1;
@synthesize _minusItem;
@synthesize _minusItem1;

- (id) init{
    self = [super init];
    if (self != nil) {
        
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        [self setupMenu];
       
        //get client state
        ClientState* cs = [ClientState sharedClientState];
        soundon = cs.soundon;
        //set up background
        bg = [CCSprite spriteWithFile:@"RoomListBack.png"];
        [bg setScale:0.5];
        bg.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:bg z:-1];
        
        //label send notification
        CCLabelTTF *notification = [CCLabelTTF labelWithString:@"Send Notification?" 
                                                      fontName:@"Marker Felt"
                                                      fontSize:30];
        notification.position = ccp(winSize.width/2 -50 , winSize.height/2+notification.contentSize.height*1.5);
        notification.color = ccc3(22, 170, 19);
        [self addChild:notification z:1];
        //label sound on;
        CCLabelTTF *sound = [CCLabelTTF labelWithString:@"Sound On" 
                                               fontName:@"Marker Felt" 
                                               fontSize:30];
        sound.position = ccp(winSize.width/2 -50, winSize.height/2 - sound.contentSize.height*1.5);
        sound.color = ccc3(22, 170, 19);
       [self addChild:sound z:1];
        
        //add sound on sprite

        [self setupSoundMenu];
        [self setupNoticeMenu];
    }
    return self;
}
    
-(void)setupSoundMenu{
   
    _plusItem = [[CCMenuItemImage itemFromNormalImage:@"rightIcon.jpg"
                                                         selectedImage:@"rightIcon.jpg" 
                                                                target:nil 
                                                              selector:nil]retain];
     _minusItem = [[CCMenuItemImage itemFromNormalImage:@"wrongIcon.gif"
                                                         selectedImage:@"wrongIcon.gif"
                                                                target:nil 
                                                              selector:nil]retain];
    CCMenuItemToggle *toggleItem = [CCMenuItemToggle itemWithTarget:self 
                                                           selector:@selector(modifySound:) items:_plusItem, _minusItem, nil];
    CCMenu *toggleMenu = [CCMenu menuWithItems:toggleItem, nil];
    CGSize winSize = [[CCDirector sharedDirector] winSize];

    toggleMenu.position = ccp(winSize.width*0.55, winSize.height*0.45);
    [self addChild:toggleMenu z:2];
}

-(void)setupNoticeMenu{
    
    _plusItem1 = [[CCMenuItemImage itemFromNormalImage:@"rightIcon.jpg"
                                        selectedImage:@"rightIcon.jpg" 
                                               target:nil 
                                             selector:nil]retain];
    _minusItem1 = [[CCMenuItemImage itemFromNormalImage:@"wrongIcon.gif"
                                         selectedImage:@"wrongIcon.gif"
                                                target:nil 
                                              selector:nil]retain];
    CCMenuItemToggle *Item = [CCMenuItemToggle itemWithTarget:self 
                                                           selector:@selector(modifyNotice:) items:_plusItem1, _minusItem1, nil];
    CCMenu *noticeMenu = [CCMenu menuWithItems:Item, nil];
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    
    noticeMenu.position = ccp(winSize.width*0.65, winSize.height*0.55);
    [self addChild:noticeMenu z:3];
}

-(void)setupMenu
{
	// Create some menu items
	CCMenuItemImage *menuItem1 = [CCMenuItemImage itemFromNormalImage:@"SaveButton.png"
														 selectedImage: @"SavePressed.png"
																target:self
                                                             selector:@selector(transit:)];
	menuItem1.tag = SAVEBUTTON;
    
	CCMenuItemImage *menuItem2 = [CCMenuItemImage itemFromNormalImage:@"QuitButton.png"
														 selectedImage: @"QuitPressed.png"
																target:self
															  selector:@selector(transit:)];
    menuItem2.tag = QUITBUTTON;
	// Create a menu and add your menu items to it
	CCMenu * myMenu = [CCMenu menuWithItems:menuItem1, menuItem2, nil];
	
	// Arrange the menu items vertically
	[myMenu alignItemsHorizontallyWithPadding:2];
    
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    myMenu.position = ccp(winSize.width*0.5, winSize.height*0.3);
	
	// add the menu to your scene
	[self addChild:myMenu z:1];
}

-(void)transit:(CCMenuItem*)sender{
    if(sender.tag == QUITBUTTON){
        //return to previous setting
        ClientState* cs = [ClientState sharedClientState];
        if(cs.soundon != self.soundon){
            [cs changeSound];
        }
        cs.soundon = self.soundon;
        

                [self.parent removeChild:self cleanup:YES];
    }
    else if(sender.tag == SAVEBUTTON){
        //save and quit
        [self.parent removeChild:self cleanup:YES];
    }
}

-(void)modifySound:(CCMenuItem*)sender{
    ClientState* cs = [ClientState sharedClientState];
    [cs changeSound];
}

-(void)modifyNotice:(CCMenuItem*)sender{
    ClientState* cs = [ClientState sharedClientState];
    [cs changeNotice];

}

-(void)dealloc{
    [bg release];
    bg = nil;
    [_plusItem release];
    _plusItem = nil;
    [_minusItem release];
    _minusItem = nil;
    [_plusItem1 release];
    _plusItem1 = nil;
    [_minusItem1 release];
    _minusItem1 = nil;
}
@end
